Method for recognition of full-word saturating addition and subtraction

ABSTRACT

A method of formulating and solving equations that facilitate recognition of full word saturating addition and subtraction The method includes formulating, for each basis addition statement z=x+y or subtraction statement z=x−y, data flow equations that describe properties of the program statements being analyzed; and solving the data flow equations. The properties may include: (a) the values BITS of program variables as Boolean functions of the sign bits of x, y and z; (b) the condition COND under which program statements are executed as Boolean functions of the sign bits of x, y and z; and (c) the condition REACH of which values of variables reach any given use of z when overflow/underflow/neither occurs.

CROSS REFERENCE

[0001] The present application claims the benefit of the U.S. provisional application entitled Method for Recognition of Full-Word Saturating Addition and Subtraction, filed Nov. 12, 2002, which is incorporated herein by reference.

FIELD OF THE INVENTION

[0002] A new method for recognition of full-word saturating addition and subtraction, in general and more specifically to a method based on solution of data-flow equations derived from the analyzed computer programs. This is in contrast to currently employed methods that are based on matching fixed patterns in program code.

BACKGROUND AND SUMMARY OF THE INVENTION

[0003] Reference made herein and listed at the end are incorporated herein as necessary for understanding and implementing of the present invention.

[0004] Saturating arithmetic is widely used in digital signal processing (DSP) applications. Full-word saturating arithmetic operations work by setting their result to a pre-defined maximum or minimum in those situations where the corresponding basic 2's complement operations overflow or underflow.

[0005] Modern digital signal processors provide fast implementations of saturating operations. At the same time, many DSP applications are written in a standard programming language (such as ANSI C) with saturating operations coded as subroutines or macros in terms of basic 2's complement operations. In order to provide fast execution of such programs it is important to have an optimizing compiler automatically recognize and convert code fragments to individual saturating operations whenever possible.

[0006] The prior art in compiling for digital signal processors with saturating operations can be divided into two categories: a) special data types or intrinsic functions are used to signal to the compiler which +/− operations should be treated as saturating; and b) pattern matching of program fragments is used to discover saturating operations. The task of automatically recognizing saturating operations is complicated by the fact that there are many ways of programming such operations. An optimizing compiler relying on simple pattern recognition would have to test for a large number of patterns.

[0007] The present invention solves the problem of recognizing saturating addition and subtraction by capturing the essence of these operations in a set of data-flow program analysis equations that can be solved using well-known techniques in the area of optimizing compilers (see Ref 1, 5, 6, 7).

[0008] The method of formulating and solving equations that facilitate recognition of full word saturating addition and subtraction, includes formulating, for each basis addition statement z=x+y or subtraction statement z=x−y, data flow equations that describe properties of the program statements being analyzed; and solving the data flow equations.

[0009] The following properties of the program can be used:

[0010] (a) the values BITS of program variables as Boolean functions of the sign bits of x, y and z;

[0011] (b) the condition COND under which program statements are executed as Boolean functions of the sign bits of x, y and z; and

[0012] (c) the condition REACH of which values of variables reach any given use of z when overflow/underflow/neither occurs.

[0013] The method includes replacing z in a statement S of the program that uses z with the use of z_(sat) if and only if all of the following conditions hold for that statement S: a) the only values of z that reach S assigns SATMAX to z when overflow has occurred, c) the only values of z that reach S assigns SATMIN to z if underflow has occurred, and d) the only values of z that reaches S is from the basis statement A if no underflow and no overflow has occurred. Finally the statements made unnecessary by the replacement of uses of z with z_(sat) are removed.

[0014] The equations for BITS, COND and REACH may use dense techniques or sparse program representation. The method values and conditions are approximated using global data flow analysis, which includes a lattice for approximating the properties (the values and conditions BITS, COND, REACH) and a system of equations relating approximations to various program points. Lattice vectors assigned to the variables can be compressed.

[0015] The REACH properties include a constant value C_(OF)(E,v) taken by the variable v when the edge E is executed, if overflow had occurred; a constant value C_(UF)(E,v) taken by the variable v when the edge E is executed, if underflow had occurred; and a Boolean flag ISZ(E,v) which indicates whether v carries the same value as the variable z in the basis statement A when the edge E is executed and neither overflow nor underflow had occurred.

[0016] These and other aspects of the present invention will become apparent from the following detailed description of the invention, when considered in conjunction with accompanying drawings.

BRIEF DESCRIPTION OF THE DRAWINGS

[0017]FIG. 1 is a flow chart of a method incorporating the principles of the present invention.

[0018]FIG. 2 is a control flow graph of an example add operation using the principles of the present invention.

DETAILED DESCRIPTION OF THE PREFERRED EMBODIMENTS

[0019] First is a description of how program are represented for the purposes of deriving the equations. The notion of full-word saturating addition and subtraction is formalized. Next, how the systems of equations are formulated and present an algorithm for recognition of full-word saturating addition and subtraction is described.

[0020] Program Representation

[0021] The source language programs are decomposed into individual procedures. The present technique is an intra-procedural in that it works on one procedure at a time, independently of others. Herein, the terms “program” and “procedure” are used interchangeably.

[0022] Each procedure is represented using its Control-Flow Graph (CFG). The nodes in the graph are program statements. The edges in the graph represent possible transfer of program flow between statements. It is assumed that compound expressions in assignment statements have been translated into sequences of simple assignment statements that involve only a single operator each. Such representation is standard and is referred to in the literature as three-address form (Ref 1).

[0023] There are four kinds of program statements:

[0024] 1. The unique entry statement.

[0025] 2. Assignment statements compute new values through arithmetic operations, perform memory loads or stores, or perform procedure calls. Without loss of generality, the description will be restricted to the assignment statements enumerated below.

[0026] Arithmetic operations OP_(B) (x, y) are parameterized by bit width. For example, signed addition of 8 bit integer values is translated into add₈ (x, y) . The arithmetic operations include saturating addition addsat_(B) and subtraction subsat_(B). These are not present in the initially lowered source program.

[0027] 3. Control statements provide alternative directions in program flow. Without loss of generality, assume that there is only two kinds of control statement: IF_(B)(v) and RETURN. Switch statement IF_(B)(v) has two descendants in the Control Flow Graph and transfers the control to the first one if the value of v, taken as a B bit integer, is non-zero, and to the second one, otherwise. Other kinds of control-flow statements (e.g. loops, multi-way switches) can be translated into sequences of assignment statements and IF statement.

[0028] There can only be one RETURN statement in a procedure. It indicates that the program control should be transferred to the calling procedure. Optionally, it can specify a value to return: RETURN(v).

[0029] 4. Merge statements are points where alternative paths in program execution merge.

[0030] Observe the following facts about such representation:

[0031] Assignment statements have exactly one incoming and one outgoing edge. The notation IN(S) and OUT(S) denote the incoming and outgoing edges, respectively, for an assignment statement S. PRE(S) and SUCC(S) denote the predecessor and successor statements, respectively.

[0032] The entry statement has no incoming edges and has exactly one outgoing edge. IN(S) and PRE(S) are meaningless for an entry statement. OUT(S) and SUCC(S) have the same meaning as for the assignment statements.

[0033] Control statements (IF and RETURN) each have exactly one incoming edge. A switch or IF has exactly two outgoing edges: OUT(S, true) and OUT(S, false)—for the true and false directions in the program. RETURN has no outgoing edges. IN(S) and PRE(S) have the same meaning for control statements as for assignment statements. For IF statements, SUCC(S, true) is the node that follows on the “true” direction and SUCC(S, false) is the node that follows on the “false” direction.

[0034] Merge statements have exactly one outgoing edge and can have many incoming edges. OUT(S) and SUCC(S) denote the outgoing edge and the successor statement. For join statements, IN(S) and PRE(S) denote sets of incoming edges. Assume that all merge statements have exactly two incoming edges. This simplifies the equations, but does not change the nature of the invention.

[0035] Possible kinds of assignment statements and their descriptions are listed in the table below: Statement Description a=addsat_(B)(b,c) B-bit saturating addition and subtraction a=subsat_(B)(b,c) (mathematically defined below). addsat and subsat not present in the source program. a=OP_(B)(b,c), Binary B-bit operations: addition, subtraction, where OP is one of: multiplication, bitwise and, bitwise or, bitwise add, sub, mul, and, exclusive or, shift left, shift right, shift right or, xor, shl, shr, arithmetic (with sign extension). shra 2's complement notation is used for arithmetic operations. (Observe that sign negation and bit inversion can be defined using sub and xor.) shra denotes arithmetic shift right with the sign bit copied rightward. shra_(B)(−1,1)=−1, always. shr denotes unsigned shift right: shr_(B)(−1,1)=2^((B − 1))−1 a=exts_(B,A)(b) Sign extends the B bit value b into A bits. If A < B, then the lowest order A bits of b are returned a=extz_(B,A)(b) Zero extends the B bits value b into A bits. If A < B, then the lowest order A bits of b are returned. a=cmpeq_(B)(b,c) Comparisons of B-bit integers: equality and a=cmpne_(B)(b,c) inequality. The result is a single condition bit. a=cmplt_(B)(b,c) Signed B-bit comparisons using 2's complement a=cmple_(B)(b,c) notation: less-that, less-that-or-equals. (Other signed comparisons can be re-written using these two.) a=ucmplt_(B)(b,c) Unsigned B-bit comparisons. a=ucmple_(B)(b,c) a=constant Copying of a B-bit constant into the variable NOP A null operation store_(B) a, b Store the lowest B bits of the variable a in the memory location given by variable b. a=load_(B) b Load the lowest B bits at memory location given by b are put into variable a. a=& b Put the address of the variable b into the variable a. a=CALL F(b, c, ....) A procedure call, either returning a value or not. CALL F(b,c, ...) a=UNDEF A special statement indicating that the value of the variable is modified in an unknown way. These statements are inserted as result of inter- procedural analysis and aliasing analysis in order to determine which calls and stores potentially modify which values (Ref 2). Given the state-of-the-art in compilers, it is assumed that such analysis has been performed and the UNDEF statements have been inserted, as the result.

[0036] Definition of Full-Word Saturating Operations

[0037] Full-word saturating addition and subtraction are defined relative to the basic B-bit 2's complement addition and subtraction as follows: addsat_(B)(x,y) = SATMIN_(B), if add_(B)(x,y) underflows SATMAX_(B), if add_(B)(x,y) overflows add_(B)(x,y), otherwise addsat_(B)(x,y) = SATMIN_(B), if sub_(B)(x,y) underflows SATMAX_(B), if sub_(B)(x,y) overflows sub_(B)(x,y), otherwise

[0038] Where the saturation values SATMIN and SATMAX are the smallest and largest numbers, respectively, that can be represented using B-bit 2's complement arithmetic: SATMIN_(B) = −2^((B-1)) SATMAX_(B) = 2^((B-1))-1

[0039] Problem

[0040] There are many possible ways of programming the full-word ADDSAT and SUBSAT operators in a high-level programming language such as ANSI C. Below are some possible (but not exclusive) implementations of ADDSAT: int Add_Saturated_1(int x, int y) {   int z = x + y;   if ((x > 0) && (y > 0) && (z < 0)) {     z = SATMAX;  /* overflow */   }   else if ((x < 0) && (y < 0) && (z > 0)) {     z = SATMIN;  /* underflow */   }   return z; } int Add_Saturated_2(int x, int y) {   int z = x + y;   int sign_x = x & SATMIN;  /* 1000...000 if negative */   int sign_y = y & SATMIN;   int sign_z = z & SATMIN;   if (!sign_x && !sign_y && sign_z) {     z = SATMAX;  /* overflow */   }   else if (sign_x && sign_y && !sign_z) {     z = SATMIN;  /* underflow */   }   return z } int Add_Saturated_3(int x, int y) {   int z = x + y;   int sign_x = x & SATMIN;  /* 1000...000 if negative */   int sign_y = y & SATMIN;   int sign_z = z & SATMIN;   if ( (˜(sign_x {circumflex over ( )} sign_y)) & (sign_x & sign_z) ) {     z = sign_x ? SATMIN : SATMAX;   }   return z; } int Add_Saturated_4(int x, int y) {   int z = x + y;   int sign_x = x & SATMIN;  /* 1000...000 if negative */   int sign_y = y & SATMIN;   int sign_z = z & SATMIN;   if ( (SATMIN {circumflex over ( )}(sign_x {circumflex over ( )} sign_y)) & (sign_x & sign_z) ) {     z = SATMAX + ((x >> 31) & 0x1);   }   return z; } int Add_Saturated_5 (int x, int y) {   int z = x + y;   int OF=(˜x & ˜y & z) >> 31;  /* arithmetic shift !*/   int UF=(x & y & ˜z) >> 31;   int N=˜(OF | UF);   return (MAX32 & OF) | (MIN32 & UF) | (z & N); }

[0041] The above program fragments differ in the way they test for the combinations of the sign bits of x, y and z in order to detect overflow and underflow.

[0042] In general, a program in three-address intermediate form that does not contain ADDSAT and SUBSAT operations is provided. The goal is to recognize program fragments that can be replaced with ADDSAT and SUBSAT operations.

[0043] Program Analysis Techniques

[0044] Let G=(N, E) be the control-flow graph. N is the set of nodes (statements), and E is the set of edges. Let ν be the set of variables in the program.

[0045] The algorithm proceeds by computing, for each addition statement add_(B) (x, y) , the set of uses of the assigned variable z that receive the saturated value. The addition statement under consideration is the basis statement. The variables x, y and z involved in the basis statement are the basis variables. The uses that receive the saturated value of z is the saturated uses. The sign bits of x, y and z are the basis sign bits and are referred to as X, Y and Z, respectively.

[0046] In order to identify saturated uses, the values of variables and the conditions under which statements are executed as Boolean functions are represented in terms of the basis sign bits in the basis statement. Also computed are values which reach a given use in the case of overflow, underflow or normal execution.

[0047] Given a basis statement, the present algorithm computes for the edges E and the variables v in the program the following properties:

[0048] BITS(E,v) is the vector of Boolean functions in terms of the basis sign bits, one function per bit of v. It describes the value of the variable at edge E.

[0049] C_(OF)(E,v) is the constant value (if any) taken by the variable v when the edge E is executed, if overflow had occurred.

[0050] C_(UF)(E,v) is the constant value (if any) taken by the variable v when the edge E is executed, if underflow had occurred.

[0051] ISZ(E,v) is the Boolean flag which says whether v carries the same value as the variable z in the basis statement when the edge E is executed and neither overflow nor underflow had occurred.

[0052] COND(E) is the condition, in terms of the basis sign bits, under which the edge E is executed.

[0053] C_(OF), C_(UF) and ISZ as a group are considered REACH properties or conditions indicating which values of the variable v reaches a use of variable z in statement S at edges E in the program for overflow or underflow or normal occurrences. They are used directly to identify saturated uses. A variable v used in an assignment or a switch statement receives a saturated value if all of the following conditions are satisfied:

[0054] C_(OF)(IN(S),v)=SATMAX_(B) or COND(IN(S), F_(OF))=0

[0055] C_(UF)(IN(S),v)=SATMIN_(B) or COND(IN(S), F_(UF))=0

[0056] ISZ(IN(S),v)=1 or COND(IN(S), F_(N))=0

[0057] In the following subsection, the lattices used in the formulation and solution of data-flow equations for the properties are defined. Then, the equations relating the properties and the source program are described.

[0058] The Lattices

[0059] The following three lattices are used:

[0060] The C_(OF) and C_(UF) properties are defined on the lattice Λconst used in constant propagation [6]). Λ_(const)={T,⊥}∪Z.

[0061] ISZ is defined on the two value Boolean lattice Λ_(bool){0, 1}. The meet operator Πbool is Boolean conjunction: 0Π_(bool)1=0

1=0. The values are ordered by 0<1.

[0062] BITS and COND properties use the lattice Λ formed out of Boolean predicates of three variables. Λ is described in detail below and is referred to simply as “the lattice” and explicitly qualify the other two lattices.

[0063] Lattice for Approximating Boolean Functions

[0064] Let X, Y, Z be the sign bits of the variables x, y, z, respectively. Let F be the set of all possible Boolean functions (predicates) in three variables X, Y, Z. 0 denotes the trivially false predicate: 0(X,Y,Z)=0. 1 denotes the trivially true predicate: 1(X,Y,Z)=1.

[0065] The lattice for approximating Boolean predicates is Λ=F∪{T, ⊥}. The comparison operator < on Λ is defined as: ⊥<F<T, for all FεF. The meet operator Π is defined as:

[0066] FΠG=⊥,for all F, GεF,

[0067] F≠GλΠ⊥=⊥ and λΠT=λ, for all λεΛ

[0068] The Boolean operations (and

, or

, xor ⊕) are extended to elements of lattice Λ, as follows (observe that 1⊕F=

F, so xor subsumes bit inversion), in the order of precedence of the rules:

[0069] (a) λ₁ op λ₂=F₁ op F₂, if F₁, F₂εF, for all Boolean operations

[0070] (b) 1

λ=1 and 0

λ=0, for all λεΛ

[0071] (c) ⊥ op λ=⊥ and T op λ=T, for all λεΛ and for all Boolean operations

[0072] Lattice Vectors

[0073] Λ^(B) is the set of all vectors of length B of lattice elements. The elements of such vectors are numbered from 0 to (B−1) and displayed with the element (B−1) on the left, as in:

[0074] [V_((B−1)), V_((B−2)), . . . , V₁, V₀]. (B−1) is the position of the sign bit. F^(B) is the set of all vectors of length B of Boolean predicates. Clearly, F^(B) ⊂Λ^(B).

[0075] The meet operation Π is extended to the vectors as:

[V_(B−1), . . . ,V₀]Π[U_(B−1), . . . , U₀]=[V_(B−1)ΠU_(B−1), . . . , V₀ΠU₀]

[0076] Evaluation and Application of Lattice Vectors

[0077] It is convenient to treat vectors of bits as vectors in F^(B). For a vector V=[V_(B−1), . . . ,V₀], the function eval(V) is defined to be the integer formed from the predicates V_(k), if they are all either 0 or 1. If any of the vector elements are not 0 or 1, then eval(V)=⊥. For example, eval([0,1,0,1])=5 and eval([X

Y,0,0,0])=⊥.

[0078] Given a vector of predicates V=[V_(B−1), . . . ,V₀]εF^(B), the application V(X,Y,Z) of V to the given values of the sign bits X, Y, Z is defined as the vector of bits:

V(X,Y,Z)=[V_(B−1)(X,Y,Z), . . . ,V₀(X,Y,Z)].

[0079] I.e., the “application of a vector” is “the vector of applications”. The application of a vector V of lattice values to a predicate F is defined as:

V(F)=V(X₁,Y₁,Z₁)Π . . . ΠV(X_(m),Y_(m),Z_(m)),

[0080] with the meet operator Π taken over all X_(k),Y_(k),Z_(k) such that F(X_(k),Y_(k),Z_(k)) is true.

[0081] Also, the inverse of eval function: V=ieval_(B)(C) is defined as vector VεF^(B) of bits, such that C=eval(V). For example, ieval₄(3)=[0,0,1,1] and ieval₄(−3)=[1,1,0,1]. The ieval function extended to the Λ_(const) lattice: ieval_(B)(⊥)=[⊥,⊥, . . . ,⊥]; ieval_(B)(T)=[T, T, . . . ,T].

[0082] Extension of Arithmetic Operations to Lattice Vectors

[0083] The B-bit arithmetic operations are extended to operations on vectors of lattice elements. Let U=[U_((B−1)), . . . , U₀] and V=[V_((B−1)), . . . , V₀] be two vectors of lattice elements abstracting the values of the variables u and v. Let w=op_(B) (u, v) be a B bit arithmetic operation on the variables. The bits of the result w are computed by applying a sequence of basic Boolean operations to the bits of the inputs u and v. The operation op_(B) is extended to Λ^(B) by performing on the elements of the input vector the same sequence of Boolean operations extended to Λ. The extended operation is op_(B).

[0084] For example, if the operation is B-bit addition (ADD), then the rules for deriving the value of the result vector mimic the rules for addition with carry:

[0085] Let C=0 (this is the carry bit)

[0086] For k from 0 to B−1:

W_(k)=U_(k)⊕V_(k)⊕C

C=(C

(U_(k)

V_(k)))

(U_(k)

V_(k))

[0087] For multiply operation, precise bit-wise definition, while possible, is not helpful, and thus the result of the multiply operation is defined to be the vector of all bottoms W=[⊥,⊥, . . . , ⊥].

[0088] Another example is the v=NOTZERO_(B) (u) operation. The vector V for the variable v has exactly one element: V=[V₀]. U=[U_((B−1)), . . . , U₀]. The vectors are related as:

V₀=U_((B−1))

U_((B−2))

. . .

U₀

[0089] The extension of other operations is straightforward.

[0090] Data Flow Properties

[0091] In the derivation of the data flow equations, the following notation for the overflow, underflow and no-overflow-and-no-underflow (“neither”) predicates are used:

[0092] If the basis statement is addition:

[0093] F_(OF)=

X

Y

Z

[0094] F_(UF)=X

Y

Z

[0095] If the basis statement is subtraction:

[0096] F_(OF)=

X

Y

Z

[0097] F_(UF)=X

Y

Z

[0098] F_(N)=

F_(OF)

F_(UF)

[0099] BITS and COND Property

[0100] Definitions

[0101] For every edge E in the program control flow graph and for every variable v, the vector valued property BITS(E,v)εΛ^(B) is defined. Let BITS(E,v)=[V_((B−1)), . . . ,V₀]. The values of the vector elements are interpreted as follows:

[0102] If V_(k)ε{1, 0}, then the k-th bit of the variable v is always constant when control passes over the edge E (“when E is executed”).

[0103] If V_(k)=FεF−{1, 0}, then the following holds

[0104] The basis statement dominates (see Ref 1) the edge E

[0105] The value of the k-th bit of the variable v equals F(X,Y,Z) when E is executed for the values of X, Y, Z obtained at the most recent execution of the basis statement preceding the execution of E.

[0106] If V_(k)=⊥, then the exact value for the bit as a function of X, Y, Z we cannot be determined.

[0107] If V_(k)=T, then the value of the approximation for this bit is yet undetermined. This value is used at intermediate stages of iterative solution of data flow equations, but it does not appear in the final solution.

[0108] The subscript notation BITS(E,v)_(k) is used to denote the k-th bit of the BITS(E,v) property.

[0109] For every edge E in the program graph, the defined condition property COND(E)εΛ is interpreted as follows:

[0110] If COND(E)=FεF, then the following holds:

[0111] The edge E is dominated by the basis statement

[0112] If the basis statement is executed and F(X,Y,Z)=1 (is true), for the values of X, Y, Z just after the basis statement is executed, then E is executed at some later point.

[0113] If COND(E)=⊥, then the condition under which E is executed can not determined.

[0114] If COND(E)=T, then the condition under which E is executed is yet to be determined.

[0115] Equations for BITS and COND

[0116] The equations for BITS and COND properties are formed from the control flow graph, according to the following rules:

[0117] The equations for the basis statement S are:

[0118] BITS(OUT(S), z)=[Z, ⊥,⊥ . . . , ⊥]

[0119] BITS(OUT(S), x)=[X, [⊥,⊥ . . . , ⊥]

[0120] BITS(OUT(S), y)=[Y, [⊥,⊥ . . . , ⊥]

[0121] COND(OUT(S))=1

[0122] BITS(OUT(S),v)=BITS(IN(S),v), for all vεν−{x,y,z}

[0123] The equations for the entry statement S are:

[0124] BITS(OUT(S), v)=[⊥, . . . , ⊥], for all vεν

[0125] COND(OUT(S))=⊥

[0126] For every merge statement S, form equations as follows: Let E₀ and E₁ be the incoming edges of S

[0127] Let E₀ and E₁ be the incoming edges of S

[0128] For every variable vεν: BITS(OUT(S),v)=BITS(E₀,v)ΠBITS(E_(m),v).

[0129] COND(OUT(S))=COND(E₀)

COND(E₁)

[0130] For every switch statement S of the form IF_(B)(v), create equations as follows:

[0131] Let BITS(IN(S), v)=[V_((B−1)), V_((B−2)), . . . , V₀]

[0132] Let P=V_((B−1))

. . .

V₀(the true branch is taken when v is non-zero)

[0133] COND(OUT(S, true))=COND(IN(S))

P

[0134] COND(OUT(S, false))=COND(IN(S))

P

[0135] For every variable vεν, for all kε[0,B−1]:

[0136] BITS(OUT(S, true), v)_(k)=BITS(IN(S),v)_(k)

[0137] BITS(OUT(S, false), v)_(k)=BITS(IN(S),v)_(k)

[0138] For every assignment statement S, other than the basis statement, of the form p=OP(q,r) where OP is an arithmetic operation:

[0139] BITS(OUT(S),p)=OP(BITS(IN(S),q), BITS(IN(S),r)), where OP denotes the extension of the arithmetic operation OP to the vectors of lattice elements.

[0140] BITS(OUT(S),v)=BITS(IN(S),v),for all vεν−{p}.

[0141] Other assignments to a variable p, including loads and calls:

[0142] BITS(OUT(S),p)=[⊥,⊥, . . . ,⊥]

[0143] BITS(OUT(S),v)=BITS(IN(S),v), for all vεν−{p}

[0144] C_(OF) AND C_(UF) Properties

[0145] The equations for the C_(OF) and C_(UF) properties are as follows.

[0146] For the basis statement and the entry statement:

[0147] C_(OF)(OUT(S),v)=C_(UF)(OUT(S),v)=⊥,

[0148] for all variables v (including x, y, z)

[0149] For an assignment statement S of the form w=OP(u,v):

[0150] Let U=BITS(IN(S),u), V=BITS(IN(S),v)

[0151] C_(OF)=eval(OP(U(F_(OF)), V(F_(OF))))

[0152] C_(UF)=eval(OP(U(F_(UF)), V(F_(UF))))

[0153] These rules have the following intuition: try to obtain the constant values, if any, that bits of the input variables carry. The vector/predicate evaluation expressions, U(F_(OF)), etc., perform just that. Then apply the operation to the resulting bits and see if a constant can be formed out of the result.

[0154] For example, consider the code fragment (assuming x is one of the operands in the basis statement, and the basis statement is 32-bit addition).

t1=(x>>31)& 0x1;

z=0x7fffffff+t1;

[0155] The BITS value for the t1 variable is [0,0, . . . ,0,X], since t1 get the sign bit of x shifted to the right. The BITS value for the constant is [0,1,1, . . . ,1]. The C_(OF) and C_(UF) values are:

C_(OF)=eval(add32([0,0, . . . ,0,X](F_(OF)), [0,1,1, . . . ,1](F_(OF))))=eval(add32([0,0, . . . ,0,0], [0,1,1, . . . ,1]))=eval([0,1,1, . . . ,1])=0x7fffffff=MAX₃₂

C_(UF)=eval(add32([0,0, . . . ,0,X](F_(UF)), [0,1,1, . . . ,1](F_(UF))))=eval(add32([0,0, . . . ,0,1], [0,1,1, . . . ,1]))=eval([1,0,0, . . . ,0])=0x80000000=MIN₃₂

[0156] This is how [0,0, . . . ,0,X](F_(OF)) and [0,0, . . . ,0,X](F_(UF)) are computed. Since it is assumed that the basis statement is addition, F_(OF) is true when X=Y=0 and Z=1. F_(UF) is true when X=Y=1 and Z=0. Therefore, in order to apply the vector [0,0, . . . ,0,X] to F_(OF), its elements are evaluate (they are all Boolean functions) for X=Y=0 and Z=1. This gives only one vector of bits: [0,0, . . . ,0,0]. Similarly, [0,0, . . . ,0,X](F_(UF))=[0,0, . . . ,0,1].

[0157] At switches, the value for C_(OF) and C_(UF) are propagated to the outgoing edges. At merge statements, the equations are formed as follows. Let E₀ and E₁ be the two incoming edges for a merge statement S. Then:

[0158] If (COND(E_(k))

F_(OF))ε{0,T},

[0159] then C_(OF)(OUT(S),v)=C_(OF)(E_(1−k),v), for k=0,1;

[0160] otherwise C_(OF)(OUT(S),v)=C_(OF)(E₀,v)ΠC_(OF)(E₁,v)

[0161] If (COND(E_(k))

F_(UF))ε{0,T},

[0162] then C_(UF)(OUT(S),v)=C_(UF)(E_(1−k),v), for k=0,1;

[0163] otherwise C_(UF)(OUT(S),v)=C_(UF)(E₀,v)ΠC_(UF)(E₁,v)

[0164] The rules state that if one of the incoming edges is executed only when overflow/underflow occurs or its condition has not yet been determined, then the value from the opposite edge is propagated.

[0165] ISZ Property

[0166] The rules for the ISZ property are:

[0167] For the entry statement S: ISZ(OUT(S),v)=0, for all variables v

[0168] For the basis statement S: ISZ(OUT(S),z)=1; ISZ(OUT(S),v)=0, for all other variables

[0169] For any other assignment statement S of the form w=op (u,v):

[0170] Let a=eval(BITS(IN(S),u)(F_(N))); b=eval(BITS(IN(S),v)(F_(N)))

[0171] If a is a constant such that op(a,v)=v, then ISZ(OUT(S),w)=ISZ(IN(S),v)

[0172] If b is a constant such that op(u,b)=u, then ISZ(OUT(S),w)=ISZ(IN(S),u)

[0173] Otherwise ISZ(OUT(S),w)=0

[0174] This rules state that if the assignment statement really acts as a copy operation under the neither-overflow-nor-underflow condition, then the value of ISZ is propagated.

[0175] For all outgoing edges E of a switch statement S: ISZ(E,v)=ISZ(IN(S),v), for all variables v

[0176] For a merge statement S with incoming edges E₀ and E₁ and the outgoing edge E:

[0177] If (COND(E_(k))

F_(N))ε{0,T}, then ISZ(E,v)=ISZ(E_(k−1),v), for k=0,1, for all variables v.

[0178] Otherwise ISZ(E,v)=ISZ(E₀,v)

ISZ(E₁,v). (Recall, that

is the meet operator for ISZ)

[0179] In summary, variables inherit the ISZ property through copies. If one side of a merge statement can only be executed either under overflow or underflow, then the ISZ property is propagated from the opposite side.

[0180] Algorithm

[0181] The overall algorithm for recognizing full-word saturating addition and subtraction is listed below and illustrated in FIG. 1. The algorithm is independent of the technique used to solve the data flow equations (see Ref 1, 5, 6, 7). RecognizeAddsatAndSubsat (CFG G) {   Initialize A to the set of all addition and subtraction     statements in G;   while (A ≠ Ø) {     S=some member of A; A=A−{S};     /* S is of the form z=add_(B)(x,y)or z=sub_(B)(x,y) */     Let B be the bit width of S;     Append, after S, the statement S′ of the form       zsat=addsat_(B)(x,y);     Compute BITS, COND properties       (using any applicable technique);     Compute C_(OF), C_(UF) and ISZ properties       (using any applicable technique);     ReplaceSaturatedUses(G, S, S′, B);     Perform dead code elimination;     Let R be the set of eliminated statements;     A=A−R;   } } ReplaceSaturatedUses (CFG G, Stmt S, Stmt S′, int B) {   Let X, Y, Z be the basis sign bits in S;   Let F_(OF) be the overflow predicate;   Let F_(UF) be the underflow predicate;   Let F_(N)=

F_(OF)

F_(UF) be the “neither” or “normal” predicate;   Let SATMAX_(B)=2^((B−1))−1; SATMIN_(B)=−2^((B−1);)   for all assignment and switch statements H {     for all variables v used in H {       Let E=IN(H)       The use is saturated if ALL of the following           conditions are satisfied:       (a) COND(E)

F_(N)=0 or ISZ(E,v)=1       (b) COND(E)

F_(OF)=0 or C_(OF)(E,v)=SATMAX_(B)       (c) COND(E)

F_(UF)=0 or C_(UF)(E,v)=SATMIN_(B)       If the use is saturated,         replace v with zsat (from S′)     }   } }

[0182] Implementation Details

[0183] The following describes the details of an implementation:

[0184] Representing Boolean Predicates

[0185] We need a efficient way to manipulate Boolean predicates, since they are at the core of our method. A Boolean predicate of three variables can be represented using an 8-bit integer. There are 2³=8 combinations of the values of the arguments of the function. The integer has a bit for every combination of the input variables. Boolean operations on three-input Boolean functions are isomorphic to bitwise operations on 8-bit integers.

[0186] Compressing Vectors of Predicates

[0187] Usually we are interested only a single bit of each value. For the variables x, y and z in the basis statement, this is the highest-order bit (the sign bit). For variables that are the results of comparisons, this is the lowest order bit. It is possible that intermediate values will carry some combination of the sign bits shifted to other positions. We speed up analysis by compressing vector values of the BITS property into triplet of the form: <k,λ,flag>, where:

[0188] k is the position of the “interesting” bit

[0189] λ is the lattice value for this position

[0190] flag indicates the other bits in the value are all 0, all 1, all λ, or otherwise

[0191] Data flow equations can be trivially modified to accommodate this representation. In effect, this representation performs lossy compression on the original vectors of lattice values, with no degradation in recognition due to the compression.

[0192] Conditional Propagation

[0193] Wegman-Zadeck style conditional propagation is used in the computation of the BITS and COND properties. The changes are straightforward and are analogous to the conditional propagation algorithm in Reference 7. During evaluation of merge statements, the meet operator is taken over the incoming edges, which are known to be executed. Herein, these are the edges E for which COND(E) is not T or 0. During evaluation of switch statements, the outgoing edge E for which COND(E) evaluates to 0is never visited by the algorithm.

[0194] Normalizing the Input Program

[0195] The basic algorithm is sensitive to presence of copies. For example, if the routine is written as: int addsat (int x, int y) {    int a=x;    int b=y;  int z = a + b;  if ( SATMIN32 & ( ˜(x {circumflex over ( )} y) & (a {circumflex over ( )} z) ) ) {   z = SATMAX32 + (b >> 31) & 0x1;  }  return z; }

[0196] then the basic algorithm fails to identify the saturated use in the return statement, since the basis statement is z=a+b, and there are no references in the code to sign bits of a or b. This difficulty is not unique to our approach. Other program analysis and optimization problems, such as common sub-expression elimination are sensitive to copies, as well.

[0197] The solution is to use some form of global value numbering (Ref 8) in order to normalize the input program.

[0198] In the present example of implementation, global value numbering and copy propagation are used. Dependence Flow Graphs is used as our intermediate representation (Ref 3). DFG construction coupled with scalar elimination also finds some equivalence between the uses of the variables a and x. In basic form of the invention, the equations can also be solved using well-known iterative techniques (Refs 5,6). Sparse methods based on the static single assignment (SSA) form as well as dependence-flow graphs (DFGs) (Refs 3,4) can be used to reduce time and space complexity of solution.

EXAMPLE 1

[0199] Consider the following computation of saturating addition: int addsat (int x, int y) {  int z = x + y;  if ( SATMIN32 & ( ˜(x {circumflex over ( )} y) & (x {circumflex over ( )} z) ) ) {   z = SATMAX32 + (x >> 31) & 0x1;  }  return z; }

[0200] The intermediate representation is shown in FIG. 2. For brevity, statements have coalesced into basic blocks. The selected values of the properties are:

[0201] BITS(IN(S6,t4))=[

(X⊕Y)

(X⊕Z), ⊥, ⊥, . . . , ⊥]. This is because t4 equals and32 (not32(xor32 (x,y)),xor32 (x,z))

[0202] BITS(OUT(S6),t5)=[

(X⊕Y)

(X⊕Z), 0, 0, . . . , 0], because the statement masked out all the bits but the leftmost.

[0203] The true side of the conditional in S7 is taken when t5≠0. This is equivalent to the disjunction of the bits of t5, which equals:

(X⊕Y)

(X⊕Z).

[0204]  Thus, COND(E1)=

(X⊕Y)

(X⊕Z) and

[0205]  COND(E2)=

COND(E1)

[0206] Observe that

(X⊕Y)

(X⊕Z)=F_(OF)

F_(UF)=

F_(N),

[0207]  therefore COND(E₁)=

F_(N) and COND(E₂)=F_(N)

[0208] BITS(IN(S10),t7)=[0,0, . . . ,0,X], since t7 equals to and32(1, shra32(x, 31)).

[0209] BITS(OUT(S10),z)=add32([0,0, . . . ,X], [0,1,1, . . . ,1]). Following addition rules:

[0210]  BITS(OUT(S10),z)=[X,

X,

X, . . . ,

X].

C_(OF)(OUT(S10),z)=eval(add32([0,0, . . . ,0,X](F_(OF)),[0,1,1, . . . , 1](F_(OF))))=eval(add32([0,0, . . . ,0,0], [0,1,1, . . . , 1]))=eval([0,1,1, . . . ,1])=SATMAX₃₂

C_(UF)(OUT(S10),z)=eval(add32([0,0, . . . ,0,X](F_(UF)),[0,1,1, . . . ,1](F_(UF))))=eval(add32([0,0, . . . ,0,1], [0,1,1, . . . ,1]))=eval([1,0,0, . . . ,0])=SATMIN₃₂

[0211] ISZ(OUT(S1),z)=1

[0212] ISZ(E₂,z)=1

[0213] Since COND(E₃)

F_(N)=

F_(N)

F_(N)=0, the ISZ property is propagated from E2 to the output of the S11 merge:

[0214]  ISZ(OUT(S11),z)=ISZ(IN(S12),z)=ISZ(E₂)=1

[0215] Similarly, C_(UF) and C_(OF) are propagated from E3 to the output of the merge. Thus, at the return statement S12:

[0216] C_(OF)(IN(S12),z)=SATMAX₃₂

[0217] C_(UF)(IN(S12),z)=SATMIN₃₂

[0218] ISZ(IN(S12),z)=1

[0219] This establishes that the use of z in S12 is saturated.

EXAMPLE 2

[0220] The analysis for the function below illustrates the propagation of the ISZ property: int addsat_5 (int x, int y) {    int z = x + y;    int OF=(˜x & ˜y & z) >> 31;    int UF=(x & y & ˜z) >> 31;    int N=˜(OF | UF);    return (SATMAX32 & OF) | (SATMIN32 & UF) | (z & N); }

[0221] Here is the intermediate representation with some of the expressions unexpanded:

[0222] S1: z=add32(x,y)

[0223] S2: OF=shra32 (and32 (notx),and32(not (y),z)), 31)

[0224] S3: UF=shra32 (and32 (x, and32(y,not (z))), 31)

[0225] S4: N=not32 (or32 (OF,UF))

[0226] S5: t1=and32 (OF, 0x7fffffff)

[0227] S6: t2=and32 (UF, 0x80000000)

[0228] S7: t3=and32 (N, z)

[0229] S8: t4=or32 (t1, or32 (t2, t3))

[0230] S9: return t4

[0231] Below are the solutions for some of the properties. For clarity, edge designations have been deleted from the properties, since every variable is defined exactly once:

[0232] BITS(OF)=[F_(OF), F_(OF), . . . , F_(OF)],

[0233]  BITS(UF)=[F_(UF), F_(UF), . . . , F_(UF)],

[0234]  BITS(N)=[F_(N), F_(N), . . . , F_(N)],

[0235]  BITS(z)=[Z, ⊥,⊥, . . . ,⊥]

[0236] Observe that BITS(OF)(F_(N))=[0,0, . . . ,0]=BITS(UF)(F_(N)), since F_(UF) and F_(OF), on one hand, and F_(N), on the other hand, are mutually exclusive.

[0237] ISZ(z)=1

[0238] eval(BITS(N)(F_(N)))=0xffffffff, therefore the and32 operation in S7 acts as a copy of z into t3 when no overflow and no underflow occurs: ISZ(t3)=1

[0239] BITS(t1)=[0,F_(OF),F_(OF), . . . ,F_(OF)],

[0240]  BITS(t2)=[F_(UF),0,0, . . . ,0]

[0241] eval(BITS(t1)(F_(N)))=0, eval(BITS(t2)(F_(N)))=0. Therefore the two or32 operations in S8 just copy the value of t3 to t4 when no overflow and no underflow occurs:

[0242]  ISZ(t4)=1

[0243] Some C_(OF) values:

C_(OF)(t1)=eval(and32(BITS(OF)(F_(OF)), [0,1,1, . . . ,1](F_(OF))))=eval(and32([1,1, . . . ,1], [0,1,1, . . . ,1]))=eval([0,1,1, . . . ,1])=SATMAX₃₂

C_(OF)(t2)=eval(and32(BITS(UF)(F_(OF)), [1,0,0, . . . ,0](F_(OF))))=eval(and32([0,0, . . . ,0], [1,0,0, . . . ,0]))=eval([0,0, . . . ,0])=0

C_(OF)(t3)=eval(and32(BITS(N)(F_(OF)), [Z, ⊥,⊥, . . . ,⊥]))=eval(and32([0,0, . . . ,0], [Z,⊥,⊥, . . . ,⊥]))=eval([0,0, . . . ,0])=0

[0244] Informally, C_(OF)(t4)=or32(MAX₃₂,0,0)=MAX₃₂

[0245] Some CUF values:

C_(UF)(t1)=eval(and32(BITS(OF)(F_(UF)), [0,1,1, . . . ,1](F_(UF))))=eval(and32([0,0, . . . ,0], [0,1,1, . . . , 1]))=0

C_(UF)(t2)=eval(and32(BITS(UF)(F_(UF)), [1,0,0, . . . ,0](F_(UF))))=eval(and32([1,1, . . . ,1], [1,0,0, . . . ,0]))=eval([1,0, . . ., 0])=SATMIN₃₂

C_(UF)(t3)=eval(and32(BITS(N)(F_(UF)), [Z,⊥,⊥, . . . ,⊥]))=eval(and32([0,0, . . . ,0], [Z,⊥,⊥, . . . ,⊥]))=eval([0,0, . . . ,0])=0

[0246] Informally, C_(UF)(t4)=or32(0,SATMIN₃₂,0)=SATMIN₃₂ C_(UF)(t4)=SATMIN32, C_(OF)(t4)=SATMAX32 and ISZ(t4)=1. Therefore, t4 carries the saturated value.

[0247] Although the present invention has been described and illustrated in detail, it is to be clearly understood that this is done by way of illustration and example only and is not to be taken by way of limitation. The spirit and scope of the present invention are to be limited only by the terms of the appended claims.

[0248] References

[0249] [1] Alfred V. Aho, Ravi Sethi, Jeffrey D. Ullman, “Compilers, principles, techniques, and tools”, Addison-Wesley, 1988

[0250] [2] Hans Zima, Barbara Chapman, “Supercompilers for Parallel and Vector Computes”, ACM Press, 1991

[0251] [3] Richard Johnson, Keshav Pingali, “Dependence Based Program Analysis”, In the Proceedings of the ACM SIGPLAN 1993 Conference of Programming Language Design and Implementation (PLDI).

[0252] [4] Ron Cytron et al, “An efficient method for computing static single assignment form”, In Conference Record of the ₁₆th Annual ACM Symposium on Principles of Programming Languages.

[0253] [5] Partic Cousot, Radhia Cousot, “Automatic Synthesis of Optimal Invariant Assertions: Mathematical Foundations”, In the Proceedings of the Symposium on Artificial Intelligence and Programming Languages, 1977.

[0254] [6] Gary Kildall, “A Unified Approach to Global Program Optimization”, In the Proceedings of the 1973 Symposium on Principles of Programming Languages.

[0255] [7] Mark Wegman, F. Kenneth Zadeck, “Constant Propagation with Conditional Branches”, In ACM Transaction on Programming Languages and System, vol. 13, No. 2, April 1991, pp. 181- 210. 

What is claimed:
 1. A method of recognizing full-word saturating addition and subtraction in a program for every basis statement A of the form z=ADD(x,y) or z=SUB(x,y ), the method comprising: inserting the statement Z_(sat)=ADDSAT(x,y) or Z_(sat)=SUBSAT(x,y) immediately following the statement A; computing bits values BITS(E,v) properties of the variable v at edges E in the program; computing condition COND(E) properties under which the edges E are executed in the program; computing reach REACH(E,v) properties indicating which values of the variable v reaches a use of variable z in statement S at edges E in the program for overflow or underflow or normal occurrences; replacing z in a statement S that uses z with the use of Z_(sat) if all of the following conditions hold for the statement S: a) the only value of z that reach S assigns SATMAX to z when overflow has occurred, b) the only value of z that reach S assigns SATMIN to z if underflow has occurred, and c) the only value of z that reaches S is from the basis statement A if no underflow and no overflow has occurred; and removing statements made unnecessary by the replacement of uses of z with Z_(sat) in the previous step.
 2. The method according to claim 1, including defining overflow, underflow and “normal” predicates for REACH as follows wherein X, Y, Z are the signs of variables x, y, z respectively: Form of the basis statement A Predicate z = ADD(x,y) z = SUB(x,y) Overflow condition F_(OF) F_(OF) =

X

Y

Z F_(OF) =

X

Y

Z Underflow condition F_(UF) = X

Y

Z F_(UF) = X

Y

Z F_(UF) No overflow and no F_(N) =

F_(OF)

F_(UF) underflow (“normal”) F_(N)


3. The method according to claim 2, wherein the REACH properties include a constant value C_(OF)(E,v) taken by the variable v when the edge E is executed, if overflow had occurred; a constant value C_(UF)(E,v) taken by the variable v when the edge E is executed, if underflow had occurred; and a Boolean flag ISZ(E,v) which indicates whether v carries the same value as the variable z in the basis statement A when the edge E is executed and neither overflow nor underflow had occurred.
 4. In a method of formulation and solving program analysis equations that facilitate recognition of full word saturating addition and subtraction, the method of formulation comprising, for each basis addition statement z=x+y or subtraction statement z=x−y, formulating data flow equations that describe the following properties of the program statements being analyzed: a) values BITS of program variables x, y, z as Boolean functions of the sign bits of X, Y and Z; b) condition COND under which program statements S are executed as Boolean functions of the sign bits of X, Y and Z; and c) condition REACH of which values of the variables reaches a use of variable z in statements S for overflow or underflow or normal occurrences.
 5. The method according to claim 4, wherein the occurrences of overflow or underflow or normal is determined as a function of the sign bits of X, Y and Z.
 6. The method according to claim 4, including replacing z in a statement S of the program that uses z with the use of Z_(sat) if and only if all of the following conditions hold for that statement S: a) the only values of z that reach S assigns SATMAX to z when overflow has occurred, c) the only values of z that reach S assigns SATMIN to z if underflow has occurred, and d) the only values of z that reaches S is from the basis statement A if no underflow and no overflow has occurred.
 7. The method according to claim 6, including removing statements made unnecessary by the replacement of uses of z with Z_(sat).
 8. The method according to claim 4, including solving the equations for BITS, COND and REACH using dense techniques.
 9. The method according to claim 4, including solving the equations for BITS, COND and REACH using sparse program representation.
 10. The method according to claim 4, wherein the values and conditions are approximated using global data flow analysis, which includes a lattice for approximating BITS, COND, REACH and a system of equations relating approximations to various program points.
 11. The method according to claim 10, including compressing of lattice vectors assigned to the variables.
 12. The method according to claim 4, wherein REACH includes a constant value C_(OF)(E,v) taken by the variable v when the edge E is executed, if overflow had occurred; a constant value C_(UF)(E,v) taken by the variable v when the edge E is executed, if underflow had occurred; and a Boolean flag ISZ(E,v) which indicates whether v carries the same value as the variable z in the basis statement A when the edge E is executed and neither overflow nor underflow had occurred.
 13. A method of formulating and solving equations that facilitate recognition of full word saturating addition and subtraction, the method comprising: formulating, for each basis addition statement z=x+y or subtraction statement z=x−y, data flow equations that describe properties of the program statements being analyzed and solving the data flow equations.
 14. The method according to claim 13, wherein the properties include: a) values BITS of program variables x, y, z as Boolean functions of the sign bits of X, Y and Z; b) condition COND under which program statements S are executed as Boolean functions of the sign bits of X, Y and Z; and c) condition REACH of which values of the variables reaches a use of variable z in statements S for overflow or underflow or normal occurrences.
 15. The method according to claim 14, wherein the occurrences of overflow or underflow or normal is determined as a function of the sign bits of X, Y and Z.
 16. The method according to claim 14, including replacing z in a statement S of the program that uses z with the use of Z_(sat) if and only if all of the following conditions hold for that statement S: a) the only values of z that reach S assigns SATMAX to z when overflow has occurred, c) the only values of z that reach S assigns SATMIN to z if underflow has occurred, and d) the only values of z that reaches S is from the basis statement A if no underflow and no overflow has occurred.
 17. The method according to claim 16, including removing statements made unnecessary by the replacement of uses of z with Z_(sat).
 18. The method according to claim 13, including solving the equations using dense techniques.
 19. The method according to claim 13, including solving the equations using sparse program representation.
 20. The method according to claim 14, wherein the values and conditions are approximated using global data flow analysis, which includes a lattice for approximating BITS, COND, REACH and a system of equations relating approximations to various program points.
 21. The method according to claim 20, including compressing of lattice vectors assigned to the variables.
 22. The method according to claim 14, wherein REACH includes a constant value C_(OF)(E,v) taken by the variable v when the edge E is executed, if overflow had occurred; a constant value C_(UF)(E,v) taken by the variable v when the edge E is executed, if underflow had occurred; and a Boolean flag ISZ(E,v) which indicates whether v carries the same value as the variable z in the basis statement A when the edge E is executed and neither overflow nor underflow had occurred. 